package business.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import model.Trunk;
import business.basic.iHibBaseDAO;
import business.basic.iHibBaseDAOImpl;
import business.dao.TrunkDAO;

@Component("trunkdao")
public class TrunkDAOImpl implements TrunkDAO {

	private iHibBaseDAO bdao = null;
	
	public TrunkDAOImpl(){
		bdao = new iHibBaseDAOImpl();
	}
	
	public void setBdao(iHibBaseDAO bdao){
		this.bdao = bdao;
	}
	
	@Override
	public int add(Trunk trunk) {
		Object obj = bdao.insert(trunk);
		if(obj!=null){
			return Integer.parseInt(obj.toString());
		}
		return 0;
	}

	@Override
	public boolean update(Trunk trunk) {
		return bdao.update(trunk);
	}

	@Override
	public boolean delete(int trunkid) {
		return bdao.delete(Trunk.class,trunkid);
	}

	@Override
	public Trunk getTrunkId(int trunkid) {
		return (Trunk)bdao.findById(Trunk.class,trunkid);
	}

	@Override
	public List<Trunk> getTrunk() {
		String hql = "from Trunk";
		return bdao.select(hql);
	}

	@Override
	public List<Trunk> getTrunkByWhere(String wherecondition, int startPage,
			int pageSize) {
		String hql = "from Trunk";
		if(wherecondition!=null && !wherecondition.equals("")){
			hql += wherecondition;
		}
		return bdao.selectByPage(hql, startPage, pageSize);
	}

	@Override
	public int getAmounTrunkByWhere(String wherecondition) {
		String hql = "select count(*) from Trunk ";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

}
